package com.p900.website.web.admin;

import com.p900.commons.domain.Platform;
import com.p900.commons.vo.BaseConditionVO;
import com.p900.website.service.PlatformService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Objects;

/**
 * Created by lenovo on 2015/5/1.
 */
@Controller("adminPlatform")
@RequestMapping("/admin")
public class PlatformController {

    @Autowired
    private PlatformService platformService;

    @RequestMapping("/platform/list")
    public String list(BaseConditionVO vo,
                       Model model){
        Page<Platform> platformPage = platformService.findAll(new PageRequest(vo.getPageNum() - 1, vo.getPageSize()));

        model.addAttribute("platforms", platformPage);
        vo.setTotalCount(platformPage.getTotalElements());
        model.addAttribute("vo", vo);

        return "admin/platform/list";
    }

    @RequestMapping("/platform/add")
    public String add(){
        return "admin/platform/add";
    }

    @RequestMapping("/platform/edit/{id}")
    public String edit(@PathVariable Long id,
                       Model model){
        model.addAttribute("platform", platformService.findPlatform(id));
        return "admin/platform/edit";
    }

    @ResponseBody
    @RequestMapping("/platform/save")
    public Object save(Platform platform){
        Assert.notNull(platform, "platform is null");
        platformService.save(platform);
        return DwzResultVo.closeCurrentAndReloadListPage(Platform.class);
    }


    @ResponseBody
    @RequestMapping("/platform/delete/{id}")
    public Object delete(@PathVariable Long id){
        platformService.delete(id);
        return DwzResultVo.successResult();
    }
}
